﻿using itemApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace itemApi.Controllers
{
    public class userController : ApiController
    {
        [HttpPost]
        public IHttpActionResult updatestatus([FromBody]Users us)
        {
            using (crmEntities db = new crmEntities())
            {
                Users theuser = db.Users.FirstOrDefault(x => x.uid == us.uid);
                if (theuser.isLockout == 0) {
                    theuser.isLockout = 1;
                    theuser.lockTime = DateTime.Now;
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new { code = 0, msg = "锁定成功" });
                    }
                    else {
                        return Json(new { code = -1, msg = "锁定失败" });

                    }
                }
                else
                {
                    theuser.isLockout = 0;
                    theuser.lockTime = null;
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new { code = 0, msg = "解冻成功" });
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "解冻失败" });

                    }
                }

            }

        }
        //删除
        [HttpPost]
        public IHttpActionResult del([FromBody]Users uid)
        {
            using (crmEntities db = new crmEntities())
            {
                var theuser = db.Users.FirstOrDefault(x => x.uid == uid.uid);
                var userrole = db.UserRoles.Where(x => x.UserId == uid.uid).ToList();
                if (userrole != null)
                {
                    foreach (UserRoles i in userrole)
                    {
                        db.UserRoles.Remove(i);
                    }
                    db.Users.Remove(theuser);
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new { code = 0, msg = "删除成功" });
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "删除失败" });

                    }
                }
                else
                {
                    db.Users.Remove(theuser);
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new { code = 0, msg = "删除成功" });
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "删除失败" });

                    }
                }

            }

        }
        // 获取用户列表
        [HttpGet]
        public IHttpActionResult getnumbertable(string loginName,string isLockout,  int page,  int limit) {
            using (crmEntities db = new crmEntities())
            {
                var list = db.Users.AsEnumerable();
                if (!string.IsNullOrEmpty(loginName))
                {
                    list = list.Where(x => x.loginName.Contains(loginName));

                }
                if (!string.IsNullOrEmpty(isLockout))
                {
                    list = list.Where(x => x.isLockout == Convert.ToInt32(isLockout));

                }

                var thelist = list.Select(x => new
                {
                    uid = x.uid,
                    phone = x.phone,
                    email = x.email,
                    createTime = x.createTime.ToString(),
                    isLockout = x.isLockout,
                    lockTime = x.lockTime.ToString(),
                    loginName = x.loginName
                }).ToList();
                var count = thelist.Count();
                return Json(new { data = thelist.OrderByDescending(x => x.uid).Skip((page - 1) * limit).Take(limit), count = count,code=0 });
            }
        }
        [HttpPost]
        public IHttpActionResult adduser([FromBody]Users us)
        {

            using (crmEntities db = new crmEntities())
            {
                us.isLockout = 0;
                us.createTime = DateTime.Now;
                us.password = "123456";
                us.errorCount = 0;
                db.Users.Add(us);
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "新增成功" });
                }
                else
                {

                    return Json(new { code = -1, msg = "新增失败" });
                }


            }
        }

        //编辑用户
        [HttpPost]
        public IHttpActionResult edituser([FromBody] Users us) {
            using (crmEntities db = new crmEntities()) {
                var theuser = db.Users.FirstOrDefault(x => x.uid == us.uid);
                theuser.email = us.email;
                theuser.phone = us.phone;
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "更新成功" });
                }
                else {
                    return Json(new { code = -1, msg = "修改失败" });
                }
            }


        }
        //重置用户
        [HttpPost]
        public IHttpActionResult resetuser([FromBody] Users us)
        {
            using (crmEntities db = new crmEntities())
            {
                var theuser = db.Users.FirstOrDefault(x => x.uid == us.uid);
                theuser.email =  "moren.email";
                theuser.phone ="moren.phone";
                theuser.isLockout = 0;
                theuser.lockTime = null;
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = " 重置成功" });
                }
                else
                {
                    return Json(new { code = -1, msg = "重置失败" });
                }
            }


        }

        //修改密码
       [HttpGet]
        public IHttpActionResult changepwd(string loginName,string pwd,string newpwd) {
            using (crmEntities DB = new crmEntities()) {
                var theuser = DB.Users.FirstOrDefault(x => x.loginName == loginName);
                if (pwd != theuser.password)
                {
                    return Json(new { code = -1, msg = "原密码输入错误" });
                }
                else {
                    theuser.password = newpwd;
                    int n = DB.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new { code = 0, msg = "您的登录信息过期，请重新登录" });
                    }
                    else {

                        return Json(new { code = -1, msg = "修改密码失败" });  

                    }

                }

            }

        }

        
        [HttpGet]
        public IHttpActionResult getrolelist(int uid) {
            using (crmEntities DB = new crmEntities())
            {
                //获取所有角色
                var rolelist = DB.Roles.AsEnumerable().Select(x=>new {
                    rid=x.rid,
                    rname=x.rname

                }).ToList();
                //获取该用户的角色
                var therolelist = DB.UserRoles.Where(x => x.UserId == uid).ToList();
                List<int> r = new List<int>();
                foreach (var item in therolelist) {
                    Roles ro = DB.Roles.FirstOrDefault(x => x.rid == item.RoleId);
                    r.Add(ro.rid);
                }
             
                return Json(new { code = 0, data1 = rolelist, data2 = r,msg="请求成功" });
            }
            }

        //添加角色
        [HttpPost]
        public IHttpActionResult add([FromBody]Roles ro) {
            using (crmEntities db = new crmEntities()) {
                var list = ro.str.Split(',').ToList();
                List<int> idlist = new List<int>();
                foreach (var item in list)
                {
                    idlist.Add(Convert.ToInt32(item));
                }

                foreach (var i in idlist)
                {
                    UserRoles therole = new UserRoles() { UserId = ro.userid, RoleId = i };
                    db.UserRoles.Add(therole);
                }
              int n=db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "修改成功" });
                }
                else {
                    return Json(new { code = -1, msg = "修改失败" });

                }
            }
        }

        //删除角色
        [HttpPost]
        public IHttpActionResult dele([FromBody]Roles ro)
        {
            using (crmEntities db = new crmEntities())
            {
                var list = ro.str.Split(',').ToList();
                List<int> idlist = new List<int>();
                idlist.Distinct();
                foreach (var item in list)
                {
                    idlist.Add(Convert.ToInt32(item));
                }
              
                foreach (var i in idlist)
                {
                    var therole = db.UserRoles.Where(x => x.UserId == ro.userid && x.RoleId == i);
                    foreach (var item in therole) {
                        db.UserRoles.Remove(item);
                    }
                }
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "修改成功" });
                }
                else
                {
                    return Json(new { code = -1, msg = "修改失败" });

                }
            }
           

        }




    }
}
